package com.cloud.cloudorder.order.config.rabbitmq;

import com.cloud.common.constant.RabbitmqOrderConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author : Luojie
 * @CreateTime : 2019/9/3
 * @Description :
 **/
@Configuration
public class DirectRabbitConfig {
 
    //队列 起名：OrderDirectQueue
    @Bean
    public Queue OrderDirectQueue() {

        return QueueBuilder
                .durable(RabbitmqOrderConstants.ORDER_DIRECT_QUEUE)
                //声明该队列的死信消息发送到的 交换机 （队列添加了这个参数之后会自动与该交换机绑定，并设置路由键，不需要开发者手动设置)
                .withArgument("x-dead-letter-exchange", RabbitmqOrderConstants.ORDER_DEAD_EXCHANGE)
                //声明该队列死信消息在交换机的 路由键
                .withArgument("x-dead-letter-routing-key", "dead.#")
                .build();
    }
 
    //Direct交换机 起名：OrderDirectExchange
    @Bean
    DirectExchange OrderDirectExchange() {
        return new DirectExchange(RabbitmqOrderConstants.ORDER_DIRECT_EXCHANGE,true,false);
    }
 
    //绑定  将队列和交换机绑定, 并设置用于匹配键：OrderDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(OrderDirectQueue()).to(OrderDirectExchange()).with(RabbitmqOrderConstants.ORDER_DIRECT_ROUTING);
    }
}